/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.OperationContentTag;
import com.kaaction.modules.mer.service.OperationContentTagService;

/**
 * 运营内容标签Controller
 * @author huanghaiming
 * @version 2017-07-13
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/operationContentTag")
public class OperationContentTagController extends BaseController {

	@Autowired
	private OperationContentTagService operationContentTagService;
	
	@ModelAttribute
	public OperationContentTag get(@RequestParam(required=false) String id) {
		OperationContentTag entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = operationContentTagService.get(id);
		}
		if (entity == null){
			entity = new OperationContentTag();
		}
		return entity;
	}
	
	@RequiresPermissions("mer:operationContentTag:view")
	@RequestMapping(value = {"list", ""})
	public String list(OperationContentTag operationContentTag, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OperationContentTag> page = operationContentTagService.findPage(new Page<OperationContentTag>(request, response), operationContentTag); 
		model.addAttribute("page", page);
		return "modules/mer/operationContentTagList";
	}

	@RequiresPermissions("mer:operationContentTag:view")
	@RequestMapping(value = "form")
	public String form(OperationContentTag operationContentTag, Model model) {
		model.addAttribute("operationContentTag", operationContentTag);
		return "modules/mer/operationContentTagForm";
	}

	@RequiresPermissions("mer:operationContentTag:edit")
	@RequestMapping(value = "save")
	public String save(OperationContentTag operationContentTag, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, operationContentTag)){
			return form(operationContentTag, model);
		}
		operationContentTagService.save(operationContentTag);
		addMessage(redirectAttributes, "保存运营内容标签成功");
		return "redirect:"+Global.getAdminPath()+"/mer/operationContentTag/?repage";
	}
	
	@RequiresPermissions("mer:operationContentTag:edit")
	@RequestMapping(value = "delete")
	public String delete(OperationContentTag operationContentTag, RedirectAttributes redirectAttributes) {
		operationContentTagService.delete(operationContentTag);
		addMessage(redirectAttributes, "删除运营内容标签成功");
		return "redirect:"+Global.getAdminPath()+"/mer/operationContentTag/?repage";
	}

}